package org.eclipse.mylyn.rememberthemilk.ui.editors;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.mylyn.internal.tasks.ui.editors.EditorUtil;
import org.eclipse.mylyn.rememberthemilk.core.RTMAttributeMapper.Attribute;
import org.eclipse.mylyn.rememberthemilk.core.model.RTMInfo.Key;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class RtmTaskEditorSummaryPart extends AbstractTaskEditorPart {

	private AbstractAttributeEditor summaryEditor;
	private Composite headerComposite;

	@Override
	public void createControl(Composite parent, FormToolkit toolkit) {
		Composite composite = toolkit.createComposite(parent);
		GridLayout layout = new GridLayout();
		layout.verticalSpacing = 5;
		composite.setLayout(layout);

		addSummaryText(composite, toolkit);
		
		createHeaderLayout(composite, toolkit);
		
		toolkit.paintBordersFor(composite);

		setControl(composite);
	}

	private void createHeaderLayout(Composite composite, FormToolkit toolkit) {
		headerComposite = toolkit.createComposite(composite);
		GridLayout layout = new GridLayout(6, false);
		layout.verticalSpacing = 1;
		layout.marginHeight = 1;
		layout.marginWidth = 1;
		headerComposite.setLayout(layout);
		
		TaskAttribute priorityAttribute = getTaskData().getRoot().getMappedAttribute(TaskAttribute.PRIORITY);
		addAttribute(headerComposite, toolkit, priorityAttribute);
		
		TaskAttribute statusAttribute = getTaskData().getRoot().getMappedAttribute(TaskAttribute.STATUS);
		addAttribute(headerComposite, toolkit, statusAttribute);
		
		TaskAttribute listAttribute = getTaskData().getRoot().getMappedAttribute(Attribute.TASKLIST.getRTMKey());
		addAttribute(headerComposite, toolkit, listAttribute);
		
		TaskAttribute createdAttribute = getTaskData().getRoot().getMappedAttribute(TaskAttribute.DATE_CREATION);
		addAttribute(headerComposite, toolkit, createdAttribute);
		
		TaskAttribute completedAttribute = getTaskData().getRoot().getMappedAttribute(TaskAttribute.DATE_COMPLETION);
		addAttribute(headerComposite, toolkit, completedAttribute);
		
		TaskAttribute postponedAttribute = getTaskData().getRoot().getAttribute(Key.POSTPONED.toString());
		addAttribute(headerComposite, toolkit, postponedAttribute);
		
		toolkit.paintBordersFor(headerComposite);
	}

	private void addAttribute(Composite composite, FormToolkit toolkit, TaskAttribute attribute) {
		AbstractAttributeEditor editor = createAttributeEditor(attribute);
		
		editor.createLabelControl(composite, toolkit);
		GridDataFactory.defaultsFor(editor.getLabelControl()).indent(6, 0).applyTo(
				editor.getLabelControl());
		
		editor.createControl(composite, toolkit);
	}

	private void addSummaryText(Composite composite, FormToolkit toolkit) {
		summaryEditor = createAttributeEditor(getTaskData().getRoot().getMappedAttribute(TaskAttribute.SUMMARY));
		if (summaryEditor != null) {
			summaryEditor.createControl(composite, toolkit);
			GridDataFactory.fillDefaults().hint(500, SWT.DEFAULT).grab(true, false).applyTo(summaryEditor.getControl());
			getTaskEditorPage().getAttributeEditorToolkit().adapt(summaryEditor);
		}
	}

}
